package br.pucrio.inf.lac.konoha.hyuga.command;

import com.espertech.esper.client.EPOnDemandQueryResult;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EventBean;

public class ListCommand extends Command {

	public ListCommand(String commandSTR, EPServiceProvider cepEngine) {
		super(commandSTR, cepEngine);
	}

	@Override
	public void execute() {
	    String size  = commandSTR.split(" ")[1];
		String query = "SELECT * FROM CC AS cell WHERE cell.size() >= " + size;
		
		EPOnDemandQueryResult result = cepEngine.getEPRuntime().executeQuery(query);
		
		if (result.getArray() == null) {
			System.out.println("0 ROWS RETURNED.");
		} else if (result.getArray().length == 0) {
			System.out.println("0 ROWS RETURNED.");
		} else {
		    System.out.println(result.getArray().length + " ROWS RETURNED");
			for (EventBean event : result.getArray()) {
				System.out.println(event.getUnderlying());
			}			
		}
		
		query  = "SELECT * FROM Clusters";
		result = cepEngine.getEPRuntime().executeQuery(query);
        
        if (result.getArray() == null) {
            System.out.println("0 CLUSTERS RETURNED.");
        } else if (result.getArray().length == 0) {
            System.out.println("0 CLUSTERS RETURNED.");
        } else {
            System.out.println(result.getArray().length + " CLUSTERS RETURNED");
            for (EventBean event : result.getArray()) {
                System.out.println(event.getUnderlying());
            }           
        }

	}

}
